package com.dgut.uhrs.service;

import com.dgut.uhrs.bean.Key;
import com.dgut.uhrs.bean.Record;
import com.dgut.uhrs.mapper.RecordMapper;
import com.dgut.uhrs.mapper.UserMapper;
import com.dgut.uhrs.vo.DataVO;
import com.dgut.uhrs.vo.RecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class RecordService {
    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private UserMapper userMapper;

    @Transactional
    public Boolean addRecord(Record record){
        if (recordMapper.addRecord(record)){
            userMapper.updateIsClockStaus(record.getUid());
            return true;
        }else {
            return false;
        }
    }

    public DataVO<RecordVO> showAllRecord(Key key){
        List<Record> records = recordMapper.showAllRecord(key);
        DataVO<RecordVO> dataVO = setDataVO(records);
        dataVO.setCount(recordMapper.countOfRecord());
        return dataVO;
    }

    public Record findRecordByUserIdAndDate(String userId, String date){
        return recordMapper.findRecordByUserIdAndDate(userId,date);
    }


    public DataVO<RecordVO> findTodayRecord(Key key){

        String dateStr=getNowDate();
        key.setDate(dateStr);
        List<Record> todayRecord = recordMapper.findTodayRecord(key);
        DataVO<RecordVO> dataVO = setDataVO(todayRecord);
        dataVO.setCount(recordMapper.countOfTodayRecord(key));

        return dataVO;
    }

    public DataVO<RecordVO> findAbnormalRecord(Key key) {

        List<Record> records = recordMapper.findAbnormal(key);
        DataVO<RecordVO> dataVO = setDataVO(records);
        dataVO.setCount(recordMapper.countOfAbnormalRecord());

        return dataVO;
    }

    public DataVO<RecordVO> findTodayAbnormalRecord(Key key) {
        String dateStr=getNowDate();
        key.setDate(dateStr);
        List<Record> records = recordMapper.findTodayAbnormal(key);
        DataVO<RecordVO> dataVO = setDataVO(records);
        dataVO.setCount(recordMapper.countOfTodayAbnormalRecord(key));
        return dataVO;
    }

    public List<Record> findTodayAbnormalRecordWithOnePage() {

        String dateStr=getNowDate();
        List<Record> records = recordMapper.findTodayAbnormalWithOnePage(dateStr);
        return records;
    }

    public String getNowDate(){
        Date date = new Date();
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date);
        // 将时分秒,毫秒域清零
        cal1.set(Calendar.HOUR_OF_DAY, 0);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);
        //将格式转换成yyyy-MM-dd
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(cal1.getTime());
    }

    public DataVO<RecordVO> setDataVO(List<Record> records){
        DataVO<RecordVO> dataVO = new DataVO<>();
        ArrayList<RecordVO> recordVOS = new ArrayList<>();
        dataVO.setCode("0");
        dataVO.setMsg("");
        for (Record record : records) {
            RecordVO recordVO = new RecordVO();
            BeanUtils.copyProperties(record,recordVO);
            recordVOS.add(recordVO);
        }
        dataVO.setData(recordVOS);
        return dataVO;
    }

    @Transactional
    public Boolean updateRecord(Key key) {
        Integer result = recordMapper.updateRecord(key);
        if (result==1){
            return true;
        }
        else {
            return false;
        }
    }

    @Transactional
    public void deleteRecord(Key key) {
        recordMapper.deleteRecord(key);
    }
}
